package com.guet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guet.DTO.NotificationDTO;
import com.guet.DTO.NotificationDraftDTO;
import com.guet.DTO.TopicSaveDTO;
import com.guet.Entity.Notification;
import com.guet.Entity.NotificationDraft;
import com.guet.VO.NotificationDraftVO;
import com.guet.VO.NotificationVO;
import com.guet.context.BaseContext;
import com.guet.mapper.NotificationMapper;
import com.guet.mapper.UserMapper;
import com.guet.service.NotificationService;
import com.guet.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NotificationServiceImpl implements NotificationService {
    @Autowired
    private NotificationMapper notificationMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询公告
     *
     * @param notificationDTO
     * @return
     */
    @Override
    public PageResult list(NotificationDTO notificationDTO) {
        PageHelper.startPage(notificationDTO.getCurrentPage(), notificationDTO.getPageSize());
        LocalDateTime beginTime = null;
        LocalDateTime endTime = null;
        if (notificationDTO.getBeginTime() != null) {
            beginTime = LocalDateTime.of(notificationDTO.getBeginTime(), LocalTime.MIN);
        }
        if (notificationDTO.getEndTime() != null) {
            endTime = LocalDateTime.of(notificationDTO.getEndTime(), LocalTime.MAX);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("title", StringUtils.isEmpty(notificationDTO.getTitle()) ? null : notificationDTO.getTitle());
        map.put("publisherName", StringUtils.isEmpty(notificationDTO.getPublisherName()) ? null : notificationDTO.getPublisherName());
        map.put("beginTime", notificationDTO.getBeginTime());
        map.put("endTime", notificationDTO.getEndTime());

        Page<NotificationVO> result = notificationMapper.list(map);
        Long total = result.getTotal();
        List<NotificationVO> records = result.getResult();

        return new PageResult(total, records);
    }

    /**
     * 保存教师端公告草稿
     *
     * @param notificationDraft
     */
    @Override
    public void saveNotificationDraft(NotificationDraftDTO notificationDraft) {
        // 保存草稿其实就是更新操作，先判断有没有历史草稿，如果有就删除，在执行插入操作
        Long userId = BaseContext.getCurrentId();
        NotificationDraft existingDraft = notificationMapper.queryDraftByUserId(userId);

        NotificationDraft draft = new NotificationDraft();
        BeanUtils.copyProperties(notificationDraft, draft);
        draft.setUserId(userId);

        if (existingDraft == null) {
            // 没有草稿，第一次保存
            draft.setCreateTime(LocalDateTime.now());
        } else {
            // 有草稿，保留创建时间并更新
            LocalDateTime createTime = existingDraft.getCreateTime();
            draft.setUpdateTime(createTime);
            notificationMapper.deleteDraftByUserId(userId);
        }
        notificationMapper.save(draft);
    }

    /**
     * 获取草稿
     * @return
     */
    @Override
    public NotificationDraftVO getNotificationDraft() {
        Long userId = BaseContext.getCurrentId();
        NotificationDraft notificationDraft = notificationMapper.queryDraftByUserId(userId);
        if (notificationDraft == null) {
            return new NotificationDraftVO();
        }
        NotificationDraftVO notificationDraftVO = new NotificationDraftVO();
        BeanUtils.copyProperties(notificationDraft, notificationDraftVO);
        return notificationDraftVO;
    }

    /**
     * 发布通知公告
     * @param notificationDraftDTO
     */
    @Override
    @Transactional
    public void publish(NotificationDraftDTO notificationDraftDTO) {
        // 插入notification表并删除草稿
        Notification notification = new Notification();
        Long userId = BaseContext.getCurrentId();
        BeanUtils.copyProperties(notificationDraftDTO, notification);
        notification.setPublisherId(userId);
        notification.setCreateTime(LocalDateTime.now());
        notification.setPublisherName(userMapper.getUserName(userId));
        notificationMapper.insert(notification);
        // 删除草稿
        notificationMapper.deleteDraftByUserId(userId);
    }

    /**
     * 删除公告
     * @param id
     */
    @Override
    public void deleteNotification(String id) {
        notificationMapper.delete(id);
    }
}
